Public Class AmountOfMoney

    'CLon machetero porque me da pereza hacerlo mejor
    Public Function Clone() As AmountOfMoney
        Return New AmountOfMoney(Me.Amount, Me.AmountCurrency)
    End Function

    Public Amount As Double
    Public AmountCurrency As Currency
    Enum ELotType
        MicroLot ' 1 000
        MiniLot ' 10 000
        StandardLot '100 000 
        UnitOfBaseCurrency ' 1
    End Enum
    Public Property LotAmount(ByVal LotType As ELotType) As Double
        Get
            Return ConvertLots(Amount, ELotType.UnitOfBaseCurrency, LotType)
        End Get
        Set(ByVal value As Double)
            Amount = ConvertLots(value, LotType, ELotType.UnitOfBaseCurrency)
        End Set
    End Property
    Public Function ConvertLots(ByVal Amount As Double, ByVal InitialLotType As ELotType, ByVal DestinyLotType As ELotType) As Double
        Dim UnitsOfBaseCurrency As Double
        Select Case InitialLotType
            Case ELotType.MicroLot
                UnitsOfBaseCurrency = Amount * 1000
            Case ELotType.MiniLot
                UnitsOfBaseCurrency = Amount * 10000
            Case ELotType.StandardLot
                UnitsOfBaseCurrency = Amount * 100000
            Case ELotType.UnitOfBaseCurrency
                UnitsOfBaseCurrency = Amount
        End Select

        Dim Rta As Double
        Select Case DestinyLotType
            Case ELotType.MicroLot
                Rta = UnitsOfBaseCurrency / 1000
            Case ELotType.MiniLot
                Rta = UnitsOfBaseCurrency / 10000
            Case ELotType.StandardLot
                Rta = UnitsOfBaseCurrency / 100000
            Case ELotType.UnitOfBaseCurrency
                Rta = UnitsOfBaseCurrency
        End Select

        Return Rta
    End Function
    Shared Function Convert(ByVal InitialAmountOfMoney As AmountOfMoney, ByVal DestinyCurrency As Currency, _
    ByVal CurrencyPairDictionary As Dictionary(Of CurrencyPair.ECurrencyPairSymbol, CurrencyPair), ByVal CurrencyDictionary As Dictionary(Of Currency.ECurrencySymbol, Currency)) As AmountOfMoney
        Dim ExchangeRate As Double = GetExchangeRate(InitialAmountOfMoney.AmountCurrency, DestinyCurrency, CurrencyPairDictionary)
        If ExchangeRate = -1 Then
            Dim USD As Currency = Currency.GetCurrencyFromString("USD", CurrencyDictionary)
            Dim ExchangeRateBaseCurrencyUSD As Double = GetExchangeRate(InitialAmountOfMoney.AmountCurrency, USD, CurrencyPairDictionary)
            Dim ExchangeRateUSDQuoteCurrency As Double = GetExchangeRate(USD, DestinyCurrency, CurrencyPairDictionary)
            If ExchangeRateUSDQuoteCurrency = -1 Or ExchangeRateBaseCurrencyUSD = -1 Then
                Throw New Exception("Currency Pair: " & InitialAmountOfMoney.AmountCurrency.Symbol.ToString & DestinyCurrency.Symbol.ToString & " wasn't found")
            Else
                ExchangeRate = ExchangeRateBaseCurrencyUSD * ExchangeRateUSDQuoteCurrency
            End If
        End If
        Return New AmountOfMoney(InitialAmountOfMoney.Amount * ExchangeRate, DestinyCurrency)
    End Function
    ''' <summary>
    ''' Returns -1 if the exchange rate wasn't found.
    ''' </summary>
    ''' <param name="BaseCurrency"></param>
    ''' <param name="QuoteCurrency"></param>
    ''' <param name="CurrencyPairDictionary"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Shared Function GetExchangeRate(ByVal BaseCurrency As Currency, ByVal QuoteCurrency As Currency, ByVal CurrencyPairDictionary As Dictionary(Of CurrencyPair.ECurrencyPairSymbol, CurrencyPair)) As Double
        Dim ExchangeRate As Double
        Dim PairFoundInReversedOrder As Boolean

        Dim currencyPair As CurrencyPair = _
            currencyPair.GetCurrencyPairFromPairOfCurrencies(CurrencyPairDictionary, _
            BaseCurrency, QuoteCurrency, PairFoundInReversedOrder)

        If currencyPair Is Nothing Then
            If BaseCurrency Is QuoteCurrency Then
                ExchangeRate = 1
            Else
                ExchangeRate = -1
            End If
        Else
            If PairFoundInReversedOrder Then
                ExchangeRate = 1 / currencyPair.ExchangeRate
            Else
                ExchangeRate = currencyPair.ExchangeRate
            End If
        End If
        Return ExchangeRate
    End Function
    Sub New(ByVal amount As Double, ByVal currency As Currency)
        Me.AmountCurrency = currency
        Me.Amount = amount
    End Sub
    Sub New(ByVal lotAmount As Double, ByVal lotType As ELotType, ByVal currency As Currency)
        Me.LotAmount(lotType) = lotAmount
        Me.AmountCurrency = currency
    End Sub


End Class
